import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void gameOfLife(int[][] board) {
        int rows = board.length;
        int cols = board[0].length;
        // 创建原板的深拷贝
        int[][] tmp = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            tmp[i] = Arrays.copyOf(board[i], cols);
        }

        // 坐标偏移量
        int[] offset = {-1, 0, 1};

        // 遍历每个细胞
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                // 统计活邻居数量
                int count = 0;
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
                        if (offset[i] == 0 && offset[j] == 0) {
                            continue;  // 跳过自身
                        }
                        int r = row + offset[i];
                        int c = col + offset[j];
                        if (r >= 0 && r < rows && c >= 0 && c < cols && tmp[r][c] == 1) {
                            count++;
                        }
                    }
                }

                // 应用生命游戏规则
                if (tmp[row][col] == 1) {
                    // 活细胞
                    if (count < 2 || count > 3) {
                        board[row][col] = 0;  // 死亡
                    }
                } else {
                    // 死细胞
                    if (count == 3) {
                        board[row][col] = 1;  // 复活
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        int[][] board = {{0,1,0},{0,0,1},{1,1,1},{0,0,0}};
        gameOfLife(board);
        System.out.println(Arrays.deepToString(board));
    }
}